package alo.p4.work;

import org.junit.Test;

import java.util.Stack;
//计算中缀表达式的值

public class Mid {
    @Test
    public void test(){
        Solution solution=new Solution();
        int s= solution.complete("1*3-(2+1)+5");
        System.out.println(s);//

    }

    class Solution{
        public  int complete(String input){
            Stack<Integer> stack1=new Stack<>();
            Stack<Character> stack2=new Stack<>();
            int num1 = 0;
            int num2 = 0;
            int stackSize = input.length();
            for (int j = 0; j < input.length(); j++) {
                char ch = input.charAt(j);
                char ch2 = input.charAt(j);
                
                if (ch2>='0'&&ch2<='9') stack1.push(ch2-48);
                else if (stack2.isEmpty()) stack2.push(ch2);
                else {
                    num1=stack1.pop();
                    num2=stack1.pop();
                    char c=stack2.pop();
                    if (c == '+'){
                        num1 = stack1.pop();
                        num2 = stack1.pop();
                        stack1.push(num2+num1);
                    }else if (c == '-'){
                        num1 = stack1.pop();
                        num2 = stack1.pop();
                        stack1.push(num2-num1);
                    }else if (c == '*'){
                        num1 = stack1.pop();
                        num2 = stack1.pop();
                        stack1.push(num2*num1);
                    }else if (c == '/') {
                        num1 = stack1.pop();
                        num2 = stack1.pop();
                        stack1.push(num2 / num1);
                    }
                    stack2.push(ch2);
                }

                
            }
            return stack1.pop();
        }
    }
    
}
